package com.shengyuan.manage_os.old.ying.erpm;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;

import com.shengyuan.manage_os.R;
import com.shengyuan.manage_os.old.ying.erpm.data.MenuItem;
import com.shengyuan.manage_os.old.ying.erpm.layout.Workspace.OnViewChangedListener;
import com.shengyuan.manage_os.old.ying.erpm.tasks.Submit_System_Setting_Data_Task;

import android.content.Intent;
import android.content.res.Resources;
import android.os.Handler;
import android.os.Message;

public class GongChengActivity extends WorkSpaceActivity implements OnViewChangedListener {

	@Override
	public List<MenuItem> LoadMenuListItems() {
    	MenuItem item; int k;
    	List<MenuItem> list = new ArrayList<MenuItem>();
    	Resources res =getResources();
    	//String s = ECApplication.pShareUtil.ReadParam(Submit_System_Setting_Data_Task.T_BiaoDan);
    	String[] gc =  Submit_System_Setting_Data_Task.A_GONGCHENGMINGCHENG_GC ;//res.getStringArray(R.array.main_menus_gc);
    	if(gc==null) return null;
    	for (int i = 0; i < gc.length; i++) {
    			String  ShiShiContent ="", ShiShiTimeE="", GongYingShang="";
    			try {
    				ShiShiContent = Submit_System_Setting_Data_Task.J_GONGCHENGMINGCHENG_GC.getJSONObject(i).getString("ShiShiContent");
    				ShiShiTimeE = Submit_System_Setting_Data_Task.J_GONGCHENGMINGCHENG_GC.getJSONObject(i).getString("ShiShiTimeE");
    				GongYingShang = Submit_System_Setting_Data_Task.J_GONGCHENGMINGCHENG_GC.getJSONObject(i).getString("GongYingShangJia");
				} catch (JSONException e) {
					e.printStackTrace();
				}
    			if ( gc[i].equals("按重量收货结算单")) {
            		item = new MenuItem();
        			item.title = "先记录总重量(毛重)单";
        			item.ShiShiTimeE = ShiShiTimeE;
        			item.ShiShiContent = ShiShiContent;
        			item.GongYingShang = GongYingShang;
        			item.index =  i;
        			item.tag = 0;
        			list.add( item);
        			
            		item = new MenuItem();
        			item.title = "记录皮重量单";
        			item.ShiShiTimeE = ShiShiTimeE;
        			item.ShiShiContent = "合并毛重单，生成按重量收货结算单";
        			item.GongYingShang = GongYingShang;
        			item.index =  i;
        			item.tag = 1;
        			list.add( item);
        			
				}else if ( gc[i].equals("按重量卖料收款单")) {
            		item = new MenuItem();
        			item.title = "先记录净重量(皮重)单";
        			item.ShiShiTimeE = ShiShiTimeE;
        			item.ShiShiContent = ShiShiContent;
        			item.GongYingShang = GongYingShang;
        			item.index =  i;
        			item.tag = 0;
        			list.add( item);
        			
            		item = new MenuItem();
        			item.title = "记录毛重量单";
        			item.ShiShiTimeE = ShiShiTimeE;
        			item.ShiShiContent = "合并皮重单，生成按重量卖料收款单";
        			item.GongYingShang = GongYingShang;
        			item.index =  i;
        			item.tag = 1;
        			list.add( item);
        			
				}else {
	        		item = new MenuItem();
	    			item.title = gc[i];
	    			item.ShiShiTimeE = ShiShiTimeE;
	    			item.ShiShiContent = ShiShiContent;
	    			item.GongYingShang = GongYingShang;
	    			item.index =  i;
	    			list.add( item);	
				}
		}
    	return list;
	}

    @Override
	protected void onStop() {
		super.onStop();
	}

	private Handler mHandler= new Handler(){

		@Override
		public void handleMessage(Message msg) {
			
			MenuItem item =(MenuItem) msg.obj;
			int index = item.index;
			String BaoDanLeiXing = "";
			try {
				Submit_System_Setting_Data_Task.mID = index;
				//表单类型
				BaoDanLeiXing = Submit_System_Setting_Data_Task.J_GONGCHENGMINGCHENG_GC.getJSONObject(index).getString("BaoDanLeiXing");
				//实施结束时间
				String value = Submit_System_Setting_Data_Task.J_GONGCHENGMINGCHENG_GC.getJSONObject(index).getString("ShiShiTimeE");
				value = value.replace("\r","");
				SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				WorkSpaceActivity.ShiShiTimeE = formatter.parse(value);
				
				//项目列表
				String JieDuanName = Submit_System_Setting_Data_Task.J_GONGCHENGMINGCHENG_GC.getJSONObject(index).getString("JieDuanName");
				String ProjectName  = Submit_System_Setting_Data_Task.J_GONGCHENGMINGCHENG_GC.getJSONObject(index).getString("ProjectName"); 
				WorkSpaceActivity.ProjectName.put(ProjectName, JieDuanName);
				
			} catch (Exception e) {
				e.printStackTrace();
			}
			Intent intent = new Intent();
			//BaoDanLeiXing =getResources().getString(R.string.title_actual_form_zhengchejiesuan);
			if (BaoDanLeiXing.equals(getResources().getString(R.string.title_actual_form_zhengchejiesuan))) {
				intent.setClass( GongChengActivity.this , Actual_Form_ZhengCheShouLiao.class);
			}else if(BaoDanLeiXing.equals(getResources().getString(R.string.title_actual_form_fangjiesuan))) {
				intent.setClass( GongChengActivity.this , Actual_Form_FangJieSuan.class);
			}else if(BaoDanLeiXing.equals(getResources().getString(R.string.title_actual_form_dunjiesuan))) {
				if (item.tag == 0) {
					intent.setClass( GongChengActivity.this , Actual_Form_ZongZhongLiang.class);
				}else {
					intent.setClass( GongChengActivity.this , Actual_Form_PiZhongLiang_2.class);
				}
			}else if(BaoDanLeiXing.equals(getResources().getString(R.string.title_actual_form_zhuangdi))) {
				intent.setClass( GongChengActivity.this , Actual_Form_ZhuangDi.class);
			}else if(BaoDanLeiXing.equals(getResources().getString(R.string.title_actual_form_xiedi))) {
				intent.setClass( GongChengActivity.this , Actual_Form_XieDi.class);
			}else if(BaoDanLeiXing.equals(getResources().getString(R.string.title_actual_form_taiban))) {
				intent.setClass( GongChengActivity.this , WorkSpaceActivityGC.class);
			}else if(BaoDanLeiXing.equals(getResources().getString(R.string.title_actual_form_jixiejishi))) {
        	    WorkSpace_TaiBanActivity_JX.ButtonIndex = 1;
        	    intent.setClass(GongChengActivity.this, WorkSpace_TaiBanActivity_JX.class);
        	    intent.putExtra("VerticalSpacing", 10);
        	    
			}else if(BaoDanLeiXing.equals(getResources().getString(R.string.title_actual_form_zhengchemailiao))) {
				intent.setClass( GongChengActivity.this , Actual_Form_ZhengCheMaiLiao.class);
			}else if(BaoDanLeiXing.equals(getResources().getString(R.string.title_actual_form_fangseller))) {
				intent.setClass( GongChengActivity.this , Actual_Form_FangMaiLiao.class);
			}else if(BaoDanLeiXing.equals(getResources().getString(R.string.title_actual_form_jixiejiayou))) {
				intent.setClass( GongChengActivity.this , Actual_Form_GC_JieXieJiaYou.class);
			}else if(BaoDanLeiXing.equals(getResources().getString(R.string.title_actual_form_dunseller))) {
				if (item.tag == 0) {
					intent.setClass( GongChengActivity.this , Actual_Form_PiZhongLiang.class);
				}else {
					intent.setClass( GongChengActivity.this , Actual_Form_ZongZhongLiang_2.class);
				}
			}else {
				return;
			}
			startActivity(intent);
		}
		
		public void handleMessage1(Message msg) {
			BaseActivity.A_GONGCHENGMINGCHENG = Submit_System_Setting_Data_Task.A_GONGCHENGMINGCHENG_GC;
			BaseActivity.J_GONGCHENGMINGCHENG = Submit_System_Setting_Data_Task.J_GONGCHENGMINGCHENG_GC;
			BaseActivity.A_GONGCHENGMINGCHENG_XIANGMU = Submit_System_Setting_Data_Task.A_GONGCHENGMINGCHENG_GC_XIANGMU;
			BaseActivity.J_GONGCHENGMINGCHENG_XIANGMU = Submit_System_Setting_Data_Task.J_GONGCHENGMINGCHENG_GC_XIANGMU;
			Intent intent = new Intent();
            switch (msg.what) {
            case 0:
        	    intent.setClass( GongChengActivity.this , Actual_Form_ZhengCheShouLiao.class);
            	break;              
            case 1:
        	    intent.setClass( GongChengActivity.this , Actual_Form_FangJieSuan.class);
            	break;
            case 2:
        	    intent.setClass( GongChengActivity.this , Actual_Form_ZongZhongLiang.class);
            	break;
            case 3:
        	    intent.setClass( GongChengActivity.this , Actual_Form_PiZhongLiang_2.class);
            	break;
            case 4:
            	intent.setClass( GongChengActivity.this , Actual_Form_ZhuangDi.class);
            	break;
            case 5:
        	    intent.setClass( GongChengActivity.this , Actual_Form_XieDi.class);
            	break;   
            case 6:
            	intent.setClass( GongChengActivity.this , WorkSpaceActivityGC.class);
            	break;
            case 7:
        	    WorkSpace_TaiBanActivity_JX.ButtonIndex = 1;
        	    intent.setClass(GongChengActivity.this, WorkSpace_TaiBanActivity_JX.class);
        	    intent.putExtra("VerticalSpacing", 10);
            	break;
            case 8:
        	    intent.setClass( GongChengActivity.this , Actual_Form_ZhengCheMaiLiao.class);
            	break;
            case 9:
        	    intent.setClass( GongChengActivity.this , Actual_Form_FangMaiLiao.class);
            	break;
            case 10:
        	    intent.setClass( GongChengActivity.this , Actual_Form_PiZhongLiang.class);
            	break; 
            case 11:
        	    intent.setClass( GongChengActivity.this , Actual_Form_ZongZhongLiang_2.class);
            	break;        
            case 12:
        	    intent.setClass( GongChengActivity.this , FinishedListActivity.class);
            	break;   
            case 13:
        	    intent.setClass(GongChengActivity.this, Actual_GC_CaiGouDan.class);
            	break;
            case 14:
            	intent.setClass(GongChengActivity.this, Actual_OA_ShouKuanDan.class);
            	break;
            case 15:        	    
            	intent.setClass(GongChengActivity.this, Actual_OA_BaoXiaoDan.class);
            	break;            	
            default:
            	return;
            }
            startActivity(intent);
		}
    	
    };


	@Override
	public Handler GetHandler() {
		return mHandler;
	}

	@Override
	public boolean IsAllow() {
		if(LoginActivity.pmUser==null)
			return false;
		if (LoginActivity.pmUser.getLogOut() == false) {
			return LoginActivity.pmUser.Department.contains("工程技术部")
					|| LoginActivity.pmUser.JiaoSe.contains("超级管理员")
					|| LoginActivity.pmUser.JiaoSe.contains("总经理")
					|| LoginActivity.pmUser.JiaoSe.contains("副总经理");
		}else {
			return false;
		}

	}
}